﻿#include <QCoreApplication>
#include <QDir>
#include "CorrectDevice.h"
#include "CGlobal.h"
#include "CModelMng.h"

#include <DbgHelp.h>
#pragma comment(lib, "Dbghelp.lib")
// 通用的异常回调函数模板
long __stdcall CommonCrashInfocallback(_EXCEPTION_POINTERS* pexcp, const QString& driverName)
{
	// 创建驱动专用的崩溃文件文件夹
	QString dumpPath = QCoreApplication::applicationDirPath() + "/DriverErrorDump/";
	QDir dir(dumpPath);
	if (!dir.exists())
	{
		dir.mkpath(".");
	}

	// 文件名包含驱动名称
	QString dumpFileName = dumpPath + driverName + "_" +
		QDateTime::currentDateTime().toString("yyyyMMddhhmmss") + ".dmp";
	std::wstring wstr = dumpFileName.toStdWString();

	// 创建dmp文件
	HANDLE hDumpFile = ::CreateFileW(
		wstr.c_str(),
		GENERIC_WRITE,
		0,
		NULL,
		CREATE_ALWAYS,
		FILE_ATTRIBUTE_NORMAL,
		NULL
	);

	if (hDumpFile != INVALID_HANDLE_VALUE)
	{
		MINIDUMP_EXCEPTION_INFORMATION dumpInfo;
		dumpInfo.ExceptionPointers = pexcp;
		dumpInfo.ThreadId = GetCurrentThreadId();
		dumpInfo.ClientPointers = TRUE;

		::MiniDumpWriteDump(
			GetCurrentProcess(),
			GetCurrentProcessId(),
			hDumpFile,
			MiniDumpNormal,
			&dumpInfo,
			NULL,
			NULL
		);
		CloseHandle(hDumpFile);
	}
	return EXCEPTION_EXECUTE_HANDLER;
}

//异常回调
long __stdcall DeviceCallback(_EXCEPTION_POINTERS* pexcp)
{
	return CommonCrashInfocallback(pexcp, "Correct");
}

// 工厂函数：创建设备实例
IDevice* createDevice(SDeviceConfig* pDevice)
{
	// 只在第一次调用时设置异常处理
	static bool bFirstCall = true;
	if (bFirstCall)
	{
		::SetUnhandledExceptionFilter(DeviceCallback);
		bFirstCall = false;
	}
	CModelMng::instance();
	if (!CModelMng::instance().m_bDbInit)
		return nullptr;
	bool bRet = false;
	CorrectDevice* pDev = new CorrectDevice(pDevice->sDeviceId,pDevice->sDeviceName,bRet);
	if (bRet)
	{
		CModelMng::instance().m_vecCorrectDev.push_back(pDev);
		return pDev;
	}
	else
	{
		delete pDev;
		pDev = nullptr;
		return nullptr;
	}
}

DEVICE_EXPORT QVector<IDevice*>* createDevices(SDeviceConfig* pDevice)
{
	CModelMng::instance();
	if (!CModelMng::instance().m_bDbInit)
		return nullptr;
	bool bRet = false;
	QVector<IDevice*>* vecDevice = new QVector<IDevice*>();
	CorrectDevice* pDev = new CorrectDevice(pDevice->sDeviceId, pDevice->sDeviceName, bRet);
	if (bRet)
	{
		CModelMng::instance().m_vecCorrectDev.push_back(pDev);
		vecDevice->push_back(pDev);
		return vecDevice;
	}
	else
	{
		delete pDev;
		pDev = nullptr;
		delete vecDevice;
		vecDevice = nullptr;
		return nullptr;
	}
}

// 销毁设备实例
void destroyDevice(IDevice* dev)
{
	delete dev;
	dev = nullptr;
}

QVector<QString>*  getModelList()
{
	static QVector<QString> modelList = { A2T("皮带跑偏") };
	return &modelList;
}

QVector<QWidget*>*  getPublicCardList()
{
	QWidget* pWidget = new QWidget();
	static QVector<QWidget*> vecWidget;
	vecWidget.push_back(pWidget);
	return &vecWidget;
}

QWidget* getPublicCard(int nCardId, QString* psCardGuid)
{
	QWidget* pWidget = new QWidget();
	return pWidget;
}

CorrectDevice::CorrectDevice(const QString &sDevGuid, const QString &sDevName, bool& bRet)
{
	m_sDevGuid = sDevGuid;
	m_pDeviceConfig = new SDeviceConfig();
	m_pDeviceConfig->sDeviceName = sDevName;
	m_pDeviceConfig->sDeviceId = sDevGuid;
	m_pModelMange = new CModelMange(sDevGuid,sDevName, bRet);
	if (bRet)
	{
		m_pModelMange->startDev();
	}
}

CorrectDevice::~CorrectDevice()
{
	if (m_pModelMange != nullptr)
	{
		delete m_pModelMange;
		m_pModelMange = nullptr;
	}
}

QObject* CorrectDevice::getDeviceObject()
{
	return nullptr;
}

bool CorrectDevice::startDevice()
{
	if (m_pModelMange != nullptr)
	{
		return m_pModelMange->startDev();
	}
	else
	{
		return false;
	}
}

void CorrectDevice::stopDevice()
{
	if (m_pModelMange != nullptr)
	{
		return m_pModelMange->stopDev();
	}
}

void CorrectDevice::deleteDevice()
{
	QString sDevId = m_sDevGuid;//上层的设备ID
	m_pModelMange->deleteDev(sDevId);
}

bool CorrectDevice::controlDevice(const QString& cmd)
{
	return false;
}

QString CorrectDevice::getDeviceStatus() const
{
	if (m_pModelMange != nullptr)
	{
		return m_pModelMange->getDeviceStatus();
	}
	return "";
}

QList<CMeasurePoint*> CorrectDevice::getMeasurePoints() const
{
	QList<CMeasurePoint*> list;
	if (m_pModelMange != nullptr)
	{
		return m_pModelMange->getMeasurePoints();
	}
	else
	{
		return list;
	}
}

void CorrectDevice::setAlarmArea(const QString& area)
{

}

QList<QString> CorrectDevice::getDeviceLogs() const
{
	QList<QString> list;
	return list;
}

QWidget* CorrectDevice::getDeviceConfigUi()
{
	if (m_pModelMange != nullptr)
	{
		return m_pModelMange->getConfigWidget();
	}
	else
	{
		return nullptr;
	}
}

QWidget* CorrectDevice::getDeviceRunUi()
{
	if (m_pModelMange != nullptr)
	{
		return m_pModelMange->getDeviceRunUi();
	}
	else
	{
		return nullptr;
	}
}

QWidget* CorrectDevice::getDeviceThumbnailUi(int nId /*= 0*/)
{
	if (m_pModelMange != nullptr)
	{
		return m_pModelMange->createWidget(EWidgetType(nId));
	}
	else
	{
		return nullptr;
	}
}

QVector<QWidget*> CorrectDevice::getDeviceUiList()
{
	QVector<QWidget*> vecRet;
	if (m_pModelMange != nullptr)
	{
		return m_pModelMange->getDeviceUiList();
	}
	else
	{
		return vecRet;
	}
}

QMap<QString,QString> CorrectDevice::getInputPointList()
{
	QMap<QString, QString> mapRet;
	if (m_pModelMange != nullptr)
	{
		return m_pModelMange->getInputPointList();
	}
	return mapRet;
}

void CorrectDevice::setInputPointList(QMap<QString, CMeasurePoint*>vecInputPoint)
{
	if (m_pModelMange != nullptr)
	{
		m_pModelMange->setInputPointList(vecInputPoint);
	}
}
